package com.example.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Work {
    private Integer id;
    private String title;//作品标题
    private String description;//描述
    private LocalDateTime publishTime;//发布时间



    private Integer userId; // 发布者的用户名
    private String imgUrl; // 作品的图片地址
    private int views; // 浏览量
    private int likes; // 点赞数
    private List<Comment> comments; // 作品的评论列表
    private String authorName;
    private String authorRealName;


    public void setImgUrl(String imgUrl) {
        if (imgUrl != null && imgUrl.startsWith("data:image")) {
            this.imgUrl = imgUrl.substring(imgUrl.indexOf(",") + 1);
        } else {
            this.imgUrl = imgUrl;
        }
    }

    public String getImgUrl() {
        if (imgUrl == null) return null;
        if (imgUrl.startsWith("data:image")) {
            return imgUrl;
        }
        return "data:image/jpeg;base64," + imgUrl;
    }

    public String getAuthorName() {
        if (authorName != null && !authorName.trim().isEmpty()) {
            return authorName;
        }
        if (authorRealName != null && !authorRealName.trim().isEmpty()) {
            return authorRealName;
        }
        return "匿名用户";
    }
}
